package net.toby200.canalbuddy.notices.text;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import net.toby200.canalbuddy.R;
import net.toby200.canalbuddy.notices.NoticeFilter;
import net.toby200.canalbuddy.notices.model.Notice;

import java.util.Collection;
import java.util.List;
import java.util.logging.Logger;

/**
 * Created by toby on 22/06/2014.
 */
public class NoticeArrayAdapter extends ArrayAdapter<Notice> implements Filterable, SharedPreferences.OnSharedPreferenceChangeListener   {
    private static final Logger LOG = Logger.getLogger(NoticeArrayAdapter.class.getName());

    private final Context context;
    private Collection<Notice> notices;

    NoticeFilter filter;

    static class ViewHolder {
        public TextView waterway;
        public TextView title;
    }

    public NoticeArrayAdapter(Context context, List<Notice> values) {
        super(context, R.layout.notice_row_view, values);
        this.context = context;
        this.notices = values;
        this.filter = new NoticeFilter(context);
    }

    @Override
    public Filter getFilter() {
        return new NoticeListFilter();
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
        getFilter().filter("");
    }

    public void onNoticeUpdate(Collection<Notice> notices) {
        LOG.info("onNoticeUpdate");
        this.notices = notices;
        getFilter().filter("");

    }

    @Override
    @SuppressWarnings("unchecked")
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = convertView;
        ViewHolder viewHolder;

        if (rowView == null) {
            rowView = inflater.inflate(R.layout.notice_row_view, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.waterway = (TextView) rowView.findViewById(R.id.row_waterway);
            viewHolder.title = (TextView) rowView.findViewById(R.id.row_title);
            rowView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) rowView.getTag();
        }

        if (position % 2 == 0) {
            rowView.setBackgroundColor(context.getResources().getColor(R.color.listRow1));
        } else {
            rowView.setBackgroundColor(context.getResources().getColor(R.color.listRow2));
        }


        viewHolder.waterway.setText(getItem(position).getWaterway());
        viewHolder.title.setText(getItem(position).getTitle());

        return rowView;
    }

    private class NoticeListFilter extends Filter {


        @Override
        @SuppressWarnings("unchecked")
        protected FilterResults performFiltering(CharSequence charSequence) {
            FilterResults result = new FilterResults();
            result.values = filter.getFilteredNotices(notices);

            result.count = ((Collection<Notice>)result.values).size();
            return result;
        }

        @Override
        @SuppressWarnings("unchecked")
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            clear();
            addAll((Collection<Notice>)filterResults.values);
            notifyDataSetChanged();
        }
    }

}
